﻿using System.Web.Mvc;
using MedicalLog.Domain.Repositories;
using MedicalLog.Domain.Entities;
using System;
using MedicalLog.WebUI.Models;

namespace MedicalLog.WebUI.Controllers
{

    public class PatientController : Controller
    {

        PatientsRepository patientRepository;
        PatientViewModel patientViewModel;
        public PatientController()
        {
            patientRepository = new PatientsRepository();
            patientViewModel = new PatientViewModel();
        }


        #region Controller Actions
        [HttpGet]
        public ActionResult AddPatient()
        {
            return View(patientViewModel);
        }



        [HttpPost]
        public ActionResult AddPatient(PatientViewModel newPatient)
        {
            if (ModelState.IsValid)
            {
                patientRepository.AddPatient(RemoveStringFormaters(newPatient.Patient));
                return RedirectToAction("searchPatient");
            }
            else
            {
                return View(newPatient);
            }
        }


        [HttpGet]
        public ActionResult SearchPatient()
        {
            return View();
        }


        public ActionResult DetailPatient(int id = 1)
        {
            return View(patientRepository.DetailPatient(id));
        }


        [HttpGet]
        public ActionResult UpdatePatient(int id = 1)
        {
            return View(patientRepository.DetailPatient(id));
        }


        [HttpPost]
        public ActionResult UpdatePatient(Patient patient)
        {

            patientRepository.UpdatePatient(RemoveStringFormaters(patient));
            return RedirectToAction("SearchPatient");
        }


        [HttpGet]
        public ActionResult TrackPatient()
        {
            return View();
        }
        #endregion


        #region Utility Methods
        public Patient RemoveStringFormaters(Patient patient)
        {

            patient.Telephone = patient.Telephone.Replace("(", "");
            patient.Telephone = patient.Telephone.Replace(")", "");
            patient.Telephone = patient.Telephone.Replace("-", "");
            patient.Telephone = patient.Telephone.Replace(" ", "");

            patient.Celphone = patient.Celphone.Replace("(", "");
            patient.Celphone = patient.Celphone.Replace(")", "");
            patient.Celphone = patient.Celphone.Replace("-", "");
            patient.Celphone = patient.Celphone.Replace(" ", "");

            patient.IdentificationNumber = patient.IdentificationNumber.Replace("-", "");
            return patient;
        }
        #endregion


        #region Controllers Ajax calls
        public JsonResult GetPatient(string id)
        {
            try
            {
                id = id.Replace("-", "");
                var patient = patientRepository.SearchPatient(id);
                if (patient != null)
                {
                    return Json(new Patient
                    {
                        Name = patient.Name,
                        LastName = patient.LastName,
                        Id = patient.Id
                    },
                    JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return CatchErrors("El paciente no ha sido encontrado.");
                }

            }
            catch (InvalidOperationException)
            {
                return CatchErrors("El paciente no ha sido encontrado.");
            }
        }


        public JsonResult CatchErrors(string message)
        {
            return Json(message, JsonRequestBehavior.AllowGet);
        }
        #endregion


    }
}
